package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Jason Whiting
 */
public class Order extends BusinessObject
{

    private Date date;
    private List<OrderLine> orderLines;
    private String serviceFacilityId;
    private ServiceFacility serviceFacility;
    private String manufacturerId;
    private Manufacturer manufacturer;

    /**
     * Creates a new instance of BusinessObject
     * @param id
     */
    public Order(String id)
    {
        super(id);
    }//constructor

    //Quick Access Method
    public void save() throws DataException
    {
        try
        {
            OrderDAO.getInstance().save(this);
        } catch (DataException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * @return the orderLines
     */
    public List<OrderLine> getOrderLines()
    {
        if (orderLines == null)
        {
            orderLines = new ArrayList<OrderLine>();
        }
        return orderLines;
    }

    /**
     * @return the date
     */
    public Date getDate()
    {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date)
    {
        this.date = date;
        setDirty();
    }

    /**
     * @param orderLines the orderLines to set
     */
    public void setOrderLines(List<OrderLine> orderLines)
    {
        this.orderLines = orderLines;
        setDirty();
    }

    /**
     * Add a orderLine
     * @param orderLine to add
     */
    public void addOrderLine(OrderLine orderLine)
    {
        getOrderLines().add(orderLine);
        setDirty(); // Set Dirty
    }

    /** Remove a order line
     * @param orderLine  to remove
     */
    public void removeOrderLine(OrderLine orderLine)
    {
        getOrderLines().remove(orderLine);
        setDirty();
    }

    /**
     * @return the serviceFacilityId
     */
    public String getServiceFacilityId()
    {
        return serviceFacilityId;
    }

    /**
     * @param serviceFacilityId the serviceFacilityId to set
     */
    public void setServiceFacilityId(String serviceFacilityId)
    {
        this.serviceFacilityId = serviceFacilityId;
        setDirty();
    }

    /**
     * @return the serviceFacility from the DAO
     */
    public ServiceFacility getServiceFacility()
    {
        if (serviceFacility == null)
        {
            try
            {
                serviceFacility = ServiceFacilityDAO.getInstance().read(serviceFacilityId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return serviceFacility;
    }

    /**
     * @param serviceFacility the serviceFacility to set
     */
    public void setServiceFacility(ServiceFacility serviceFacility)
    {
        this.serviceFacility = serviceFacility;
        this.serviceFacilityId = serviceFacility.getId();
        setDirty(true);
    }

    /**
     * @return the manufacturerId
     */
    public String getManufacturerId()
    {
        return manufacturerId;
    }

    /**
     * @param manufacturerId the manufacturerId to set
     */
    public void setManufacturerId(String manufacturerId)
    {
        this.manufacturerId = manufacturerId;
        setDirty(true);
    }

    /**
     * @return the manufacturer from the DAO
     */
    public Manufacturer getManufacturer()
    {
        if (manufacturer == null)
        {
            try
            {
                manufacturer = ManufacturerDAO.getInstance().read(manufacturerId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return manufacturer;
    }

    /**
     * @param manufacturer the manufacturer to set
     */
    public void setManufacturer(Manufacturer manufacturer)
    {
        this.manufacturer = manufacturer;
        this.manufacturerId = manufacturer.getId();
        setDirty(true);
    }
}
